layui.use(['form', 'layer', 'table', 'admin'], function () {
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var $ = layui.jquery;
    var table = layui.table;
    var admin = layui.admin;
    var prefix = ctx + "system/operLog";

    // 渲染操作日志表格
    var tableIns = table.render({
        elem: '#roleList',
        url: prefix + '/operLogList',
        toolbar: '#toolbar',
        method: 'post',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 20, 30],
        limit: 10,
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'operId', title: '日志id', minWidth: 100, align: "center"},
            {field: 'title', title: '模块标题', minWidth: 200, align: 'center'},
            {field: 'businessType', title: '业务类型', minWidth: 200, align: 'center', templet: function (d) {
                    if (d.businessType === 1) {
                        return '新增';
                    } else if (d.businessType === 2){
                        return '修改';
                    }else if (d.businessType === 3){
                        return '删除';
                    }else {
                        return '其他';
                    }
                }
            },
            {field: 'method', title: '方法名称', minWidth: 200, align: 'center'},
            {field: 'operatorType', title: '操作类别', minWidth: 200, align: 'center', templet: function (d) {
                if (d.operatorType === 1) {
                    return '后台用户';
                } else if (d.operatorType === 2){
                    return '手机端用户';
                }else {
                    return '其他';
                }
            }},
            {field: 'operName', title: '操作人员', minWidth: 200, align: 'center'},
            {field: 'deptName', title: '操作地点', minWidth: 200, align: 'center'},
            {field: 'operTime', title: '操作时间', minWidth: 200, align: 'center'},
            {field: 'operUrl', title: '请求url', minWidth: 200, align: 'center'},
            {field: 'operIp', title: '主机地址', minWidth: 200, align: 'center'},
            {
                field: 'status', title: '状态', align: 'center', templet: function (d) {
                    if (d.status === 0) {
                        return '<span class="layui-badge layui-bg-green">正常</span>';
                    } else {
                        return '<span class="layui-badge layui-bg-red">异常</span>';
                    }
                }
            },
            {fixed: 'right', title: '操作', toolbar: '#bar', align: 'center', width: 150}
        ]],
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                // "msg": res.message, //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.rows //解析数据列表
            };
        }

    });

    //搜索
    $(".search_btn").on("click", function () {
        table.reload("roleList", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                roleName: $("#searchRName").val()
            }
        })
    });

    //头工具栏事件
    table.on('toolbar(roleList)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                console.log("add");
                showAddOrEditModel();
                break;
            case 'edit':
                console.log("edit");
                console.log("checkStatus:" + checkStatus);
                dataArr = checkStatus.data;
                if(dataArr.length > 0 && dataArr.length==1){
                   console.log(dataArr[0].postId);     //修改操作
                    var data = dataArr[0];
                    showAddOrEditModel(data);
                }
                else{
                    layer.msg("请选择一条数据!");
                }

                break;
            case 'del':
                console.log("del");
                console.log("checkStatus:" + checkStatus);
                data = checkStatus.data, inputs = [];
                if(data.length > 0) {
                    for (var i in data) {
                        inputs.push(data[i].roleId);
                    }
                    layer.confirm('确定删除选中的数据吗？', {icon: 3, title: '提示信息'}, function (index) {
                        console.log("inputs:"+inputs);
                        doDelete(inputs);//删除操作
                        layer.close(index);
                    })
                }else{
                    layer.msg("请选择需要删除的数据!");
                }

                break;
        }
        ;
    });

    //监听行工具事件
    table.on('tool(roleList)', function (obj) {
        var data = obj.data;
        console.log("data:" + data)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么?', function (index) {
                console.log("del");
                doDelete(data.roleId);//删除操作
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            showAddOrEditModel(data);
            console.log("edit");
        }
    });

    //新增或者修改岗位信息的弹框
    function showAddOrEditModel(data) {
        admin.putTempData('t_role', data);
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: data ? '修改角色' : '添加角色',
            area: ['500px', '550px'],
            content: '/system/role/editForm',
            end: function () {
                console.log("end");
                admin.getTempData('formOk') && table.reload('roleList');  // 成功刷新表格
            }
        });
    }

    //删除数据
    function doDelete(inputs){
        var ids = inputs.join(',');
        console.log("ids:"+ids);
        $.post(prefix+'/remove', {
            ids: ids
        }, function (data) {
            if (data.code == 0) {
                layer.msg(data.msg, {icon: 1});
                table.reload('roleList');  // 成功刷新表格
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        });
    }


});